package com.atguigu.yygh.common.handler;


import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.config.YyghException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @Author zhmwi
 * @Date 2021/6/29
 * @Dersciption 统一异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 捕获全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.error();
    }

    /**
     * 处理特定异常
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("执行了特定异常");
    }

    @ExceptionHandler(YyghException.class)
    @ResponseBody
    public R error(YyghException e){
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}